<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class jurusan extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_jurusan');
        // load library
        $this->load->library('tnotification');
    }

    // halaman list data
    public function index() {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/jurusan/index.html");
        // data 
        $rs_id = $this->m_jurusan->get_data_jurusan();
        $this->smarty->assign('rs_id', $rs_id);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // halaman tambah data
    public function add() {
        // set page rules
        $this->_set_page_rule("C");
        // set template content
        $this->smarty->assign("template_content", "master/jurusan/add.html");
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses tambah data
    public function add_process() {
        // set page rules
        $this->_set_page_rule("C");
        // cek input
        $this->tnotification->set_rules('jurusan_nm', 'Nama Jurusan', 'trim|required|maxlength[50]');
        $this->tnotification->set_rules('jurusan_alias', 'Nama Singkatan', 'trim|required|maxlength[10]');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $jurusan_nm = $this->input->post('jurusan_nm');
            $jurusan_alias = $this->input->post('jurusan_alias');
            //--
            $params = array(strtoupper($jurusan_nm), strtoupper($jurusan_alias), $this->com_user['user_id']);
            // insert data
            if ($this->m_jurusan->insert_data_jurusan($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/jurusan/add");
    }

    // halaman edit data
    public function edit($jurusan_kd = "") {
        // set page rules
        $this->_set_page_rule("U");
        // set template content
        $this->smarty->assign("template_content", "master/jurusan/edit.html");
        // data
        $result = $this->m_jurusan->get_detail_data_jurusan($jurusan_kd);
        $this->smarty->assign('result', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses edit
    public function edit_process() {
        // set page rules
        $this->_set_page_rule("U");
        // cek input
        $this->tnotification->set_rules('jurusan_nm', 'Nama Jurusan', 'trim|required|maxlength[50]');
        $this->tnotification->set_rules('jurusan_alias', 'Nama Singkatan', 'trim|required|maxlength[10]');
        $this->tnotification->set_rules('jurusan_kd', 'Kode Jurusan', 'required');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $jurusan_nm = $this->input->post('jurusan_nm');
            $jurusan_alias = $this->input->post('jurusan_alias');
            $jurusan_kd = $this->input->post('jurusan_kd');
            //--
            $params = array(
                strtoupper($jurusan_nm), strtoupper($jurusan_alias),
                $this->com_user['user_id'], $jurusan_kd
            );
            // update data
            if ($this->m_jurusan->update_data_jurusan($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/jurusan/edit/" . $this->input->post('jurusan_kd'));
    }

    // halaman hapus data
    public function delete($jurusan_kd = "") {
        // set page rules
        $this->_set_page_rule("D");
        // set template content
        $this->smarty->assign("template_content", "master/jurusan/delete.html");
        // data
        $result = $this->m_jurusan->get_detail_data_jurusan($jurusan_kd);
        $this->smarty->assign('result', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses hapus data
    public function delete_process() {
        // set page rules
        $this->_set_page_rule("D");
        // cek input
        $this->tnotification->set_rules('jurusan_kd', 'Kode Jurusan', 'required');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $jurusan_kd = $this->input->post('jurusan_kd');
            // delete data
            if ($this->m_jurusan->delete_data_jurusan($jurusan_kd)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil dihapus");
                // redirect
                redirect("master/jurusan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/jurusan/delete/" . $this->input->post('jurusan_kd'));
    }

}